package com.allen.common.component;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Autor lwl
 * 日期    2021/9/28
 * 目的    启动倒计时
 */
public class CircleNavView extends View {
    Paint mPaint = new Paint();
    public int mCircleWidth = 20;
    public int angle = 360;
    public int mProgress = 0;
    public int second = 5;
    public int per = 360 / second;
    public boolean isDrawCircle = true;


    public CircleNavView(Context context) {
        super(context);
        initView();
    }

    public CircleNavView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CircleNavView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        service.execute(runnable);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2;
        int radius = center - mCircleWidth;
        mPaint.setStrokeWidth(5); // 设置圆环的宽度
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE); // 设置空心
        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius); // 用于定义的圆弧的形状和大小的界限
        mPaint.setColor(Color.RED); // 设置圆环的颜色
        canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根据进度画圆弧
    }

    final ExecutorService service = Executors.newCachedThreadPool();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (isDrawCircle) {

                if (mProgress >= 360) {
                    isDrawCircle = false;
                } else {

                    try {
                        Thread.sleep(200); //通过传递过来的速度参数来决定线程休眠的时间从而达到绘制速度的快慢
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mProgress += 360 / 25;
                    isDrawCircle = true;
                    Log.e("xxx", "xxxxxx = " + mProgress + "");
                    postInvalidate();
                }
            }
        }
    };

}
